package com.biboheart.adapter.express.support;

import com.biboheart.adapter.express.actuator.GenericActuator;
import com.biboheart.adapter.support.utils.ObjectTypeUtils;

import java.math.BigDecimal;

final class AdditionActuator implements GenericActuator {
    @Override
    public Object execute(Object source, Object... args) {
        if (null == source) {
            return null;
        }
        if (args.length == 0) {
            return source;
        }
        Object addend = args[0];
        if (!(source instanceof Number) || !(addend instanceof Number)) {
            return source + ObjectTypeUtils.convertType(addend, String.class);
        }
        if ((source instanceof Double) || (addend instanceof Double)) {
            return BigDecimal.valueOf(ObjectTypeUtils.convertType(source, Double.class)).add(BigDecimal.valueOf(ObjectTypeUtils.convertType(addend, Double.class))).doubleValue();
        }
        if ((source instanceof Float) || (addend instanceof Float)) {
            return BigDecimal.valueOf(ObjectTypeUtils.convertType(source, Float.class)).add(BigDecimal.valueOf(ObjectTypeUtils.convertType(addend, Float.class))).floatValue();
        }
        if ((source instanceof Long) || (addend instanceof Long)) {
            return BigDecimal.valueOf(ObjectTypeUtils.convertType(source, Long.class)).add(BigDecimal.valueOf(ObjectTypeUtils.convertType(addend, Long.class))).longValue();
        }
        return BigDecimal.valueOf(ObjectTypeUtils.convertType(source, Integer.class)).add(BigDecimal.valueOf(ObjectTypeUtils.convertType(addend, Integer.class))).intValue();
    }
}
